<?php

namespace App\Http\Controllers\Section;

use App\Models\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CourseSection;
use Illuminate\Support\Facades\DB;
class CourseSectionController extends Controller
{

    public function changeState(Request $request){
        $id = $request->input('id');
        $state = $request->input('state');
        $section = CourseSection::where('id',$id)->first();
        $section->state  = $state;
        if($section->save()){
            return $this->getMessage(0,'修改成功');
        }else{
            return $this->getMessage(1,'修改状态失败');
        }
    }

    public function delete(Request $request){
        $id = $request->input('id');

        DB::beginTransaction();
        try{
            CourseSection::where('id',$id)->delete();
            Course::where('course_class_section_id',$id)->delete();
            DB::commit();
            return $this->getMessage(0,'删除成功');
        }catch(\Exception $e){
            logger()->error($e->getMessage());
            DB::rollback();
            return $this->getMessage(0,'删除失败');
        }
    }
}
